package com.stx.blog.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.stx.blog.dao.UserDao;
import com.stx.blog.exception.BlogException;
import com.stx.blog.vo.FriendVo;
import com.stx.blog.vo.UserVo;

public class UserDaoImpl extends JdbcDaoSupport implements UserDao {

	@Override
	public void addUser(UserVo user)throws Exception {
		JdbcTemplate jt = getJdbcTemplate();
		String sql = "insert into bloguser values(buid.nextval,?,?,?,?,?,?)";
		Object args[] = { user.getBlogName(), user.getBlogPassword(),
				user.getBlogEmail(), user.getBlogDescription(), user.getBlogCreatetime(),
				user.getBlogLastmodifytime()};
		jt.update(sql, args);
	}

	@Override
	public UserVo getUser(UserVo user) {
       JdbcTemplate jt=getJdbcTemplate();
       String sql="select * from bloguser where bemail=? and bpassword =?";
       Object args[]={user.getBlogEmail(),user.getBlogPassword()};
     return   (UserVo)jt.query(sql, args, new UserVoResultSetExtractor());
	}
    class UserVoResultSetExtractor implements ResultSetExtractor{

		@Override
		public Object extractData(ResultSet rs) throws SQLException,
				DataAccessException {
			UserVo uv=new UserVo();
			if(rs.next()){
			uv.setBuid(rs.getInt("buid"));
			uv.setBlogName(rs.getString("blogname"));
			uv.setBlogPassword(rs.getString("bpassword"));
			uv.setBlogEmail(rs.getString("bemail"));
			uv.setBlogDescription(rs.getString("bdescription"));
			uv.setBlogCreatetime(rs.getTimestamp("createtime"));
			uv.setBlogLastmodifytime(rs.getTimestamp("lastmodifytime"));
			return uv;
			}
			return null;
		}
    	
    }
	class UserVoRowMappser implements RowMapper{

		@Override
		public Object mapRow(ResultSet rs, int arg1) throws SQLException {
		UserVo uv=new UserVo();
		uv.setBuid(rs.getInt("buid"));
		uv.setBlogName(rs.getString("blogname"));
		uv.setBlogPassword(rs.getString("bpassword"));
		uv.setBlogEmail(rs.getString("bemail"));
		uv.setBlogDescription(rs.getString("bdescription"));
		uv.setBlogCreatetime(rs.getDate("createtime"));
		uv.setBlogLastmodifytime(rs.getDate("lastmodifytime"));
		return uv;
		}
		
	}
	@Override
	public int isBlogName(String blogName) {
		JdbcTemplate jt = getJdbcTemplate();
		String sql="select count(*) from bloguser where blogname =?";
		String args[]={blogName};
		return jt.queryForInt(sql, args);
	}

	@Override
	public void changeBase(UserVo user)throws Exception {
		JdbcTemplate jt = getJdbcTemplate();
		String sql="update bloguser set blogname=?,bdescription=?,lastmodifytime=? where buid=?";
		Object args[]={user.getBlogName(),user.getBlogDescription(),user.getBlogLastmodifytime(),user.getBuid()};
		jt.update(sql, args);
	}

	@Override
	public void changePassword(UserVo user) {
		JdbcTemplate jt = getJdbcTemplate();
		String sql="update bloguser set bpassword =?,lastmodifytime=? where buid=?";
		Object args[]={user.getBlogPassword(),user.getBlogLastmodifytime(),user.getBuid()};
		jt.update(sql, args);
		
	}

	@Override
	public UserVo getUsesr(int buid) {

		 JdbcTemplate jt=getJdbcTemplate();
	       String sql="select * from bloguser where buid=?";
	       Object args[]={buid};
	     return   (UserVo)jt.query(sql, args, new UserVoResultSetExtractor());
	}

	@Override
	public List<UserVo> getList(UserVo uv) {
		JdbcTemplate jt=getJdbcTemplate();
	    String sql="select * from bloguser where buid !=?";
	    Object args[]={uv.getBuid()};
	    return jt.query(sql, args, new UserVoRowMappser());
	}
/**
 * 好友操作
 */
	@Override
	public List<FriendVo> getFriendList(UserVo uv) {
		JdbcTemplate jt=getJdbcTemplate();
	    String sql="select * from relation where buid =?";
	    Object args[]={uv.getBuid()};
	    return jt.query(sql, args, new FriendRowMappser());
	}
	class FriendRowMappser implements RowMapper{

		@Override
		public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
			FriendVo fv=new FriendVo();
			fv.setBuid(arg0.getInt("buid"));
			fv.setFrinendid(arg0.getInt("frinendid"));
			fv.setRid(arg0.getInt("rid"));
			return fv;
			
		}
		
		
	}
	@Override
	public void addFriend(Integer buid, Integer rid) {
		JdbcTemplate jt=getJdbcTemplate();
	    String sql="insert into relation(rid,buid,frinendid) values(rid.nextval,?,?)";
	    Object args[]={buid,rid};
	    jt.update(sql, args);
	}

	

	
}
